package com.hzzy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzzy.IDao.HzClassCheckinsMapper;
import com.hzzy.bean.HzClassCheckins;
import com.hzzy.enums.CheckinsStateEnum;
import com.hzzy.service.HzClassCheckinsService;

@Service
public class HzClassCheckinsServiceImpl implements HzClassCheckinsService {

	@Autowired
	private HzClassCheckinsMapper hzClassCheckinsMapper;
	
	public int recordCheckin(String classId, String checkStudentSno, String curseId, String section, String teacherId,
			String students) {
		Date date = new Date();
		int insertCount = 0;
		// 判断是否已经点到过了
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("classId", classId);
		conditions.put("section", section);
		conditions.put("currentDate", date);
		int recordCount = hzClassCheckinsMapper.checkUpCheckinsRecourd(conditions);

		// 如果没有记录,则新增点到记录
		if (recordCount == 0) {
			List<HzClassCheckins> list = new ArrayList<HzClassCheckins>();
			//全到记录的时候
			if (StringUtils.isNotEmpty(checkStudentSno) && !students.contains("-")) {
				HzClassCheckins checkin = new HzClassCheckins();
				checkin.setStudentId(checkStudentSno);
				checkin.setCourseid(Integer.parseInt(curseId));
				checkin.setSection(section);
				checkin.setTeacherId(teacherId);
				checkin.setCcstate(CheckinsStateEnum.COMPLETE.getValue());
				checkin.setCcdate(date);
				list.add(checkin);
			} else {
				// 有学生异常上课点到的时候
				String[] sno_states = students.split(","); 	//  001_2,002_2
				if (sno_states.length > 0) {
					for (String s : sno_states) {
						String[] sno_state = s.split("-");
						if (sno_state.length == 2) {
							HzClassCheckins checkin = new HzClassCheckins();
							checkin.setStudentId(sno_state[0]);
							checkin.setCourseid(Integer.parseInt(curseId));
							checkin.setSection(section);
							checkin.setTeacherId(teacherId);
							checkin.setCcstate(Integer.parseInt(sno_state[1]));
							checkin.setCcdate(date);
							list.add(checkin);
						}
					}
				}
			}
			// 批量新增
			if (list.size() > 0) {
				insertCount = hzClassCheckinsMapper.batchInset(list);
			}
		} else if(StringUtils.isNotEmpty(students) && students.split("-").length == 3) {
			 Map<String, Object>_conditions = new HashMap<String, Object>();
			 List<HzClassCheckins> insertlist = new ArrayList<HzClassCheckins>();
			// 修改签到状态
			String[] sno_states = students.split(","); 	//  001-2-为什么,002-2-爱仕达
			List<String> snoList = new ArrayList<String>();
			for (String s : sno_states) {
				String[] sno_state = s.split("-");
				if (sno_state.length == 3) {
					// 判断点到 记录表中是否包含 该学生，如果不包含则 新增，包含则更新
					conditions.put("studentId", sno_state[0]);
					int count = hzClassCheckinsMapper.checkUpCheckinsRecourd(conditions);
					if (count == 0) {
						HzClassCheckins checkin = new HzClassCheckins();
						checkin.setStudentId(sno_state[0]);
						checkin.setCourseid(Integer.parseInt(curseId));
						checkin.setSection(section);
						checkin.setTeacherId(teacherId);
						checkin.setCcstate(Integer.parseInt(sno_state[1]));
						checkin.setCcdate(date);
						insertlist.add(checkin);
					} else {
					//  这些值都是一样的
						_conditions.put("courseId", curseId);
						_conditions.put("section", section);
						_conditions.put("teacherId", teacherId);
						// 修改状态
						_conditions.put("cstate", sno_state[1]);
						// 修改原因
						_conditions.put("cause", sno_state[2]);
						_conditions.put("currentDate", date);
						snoList.add(sno_state[0]);
					}
				}
			}
			if (snoList.size() > 0) {
				_conditions.put("snoList", snoList);
				insertCount = hzClassCheckinsMapper.updateState(_conditions);
			}
			if (insertlist.size() > 0) {
				insertCount = hzClassCheckinsMapper.batchInset(insertlist);
			}
		} else {
			insertCount = -1;
		}
		return insertCount;
	}

	public List<HzClassCheckins> getCheckinsInfoListByClassId(int classId) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("classId", classId);
		conditions.put("currentDate", new Date());
		
		return hzClassCheckinsMapper.selectByConditions(conditions);
	}

	/**
	 * 根据点到的条件，获取异常点到的学生的id
	 * @param section	上课节次
	 * @param courseid 	课程
	 * @param date		日期
	 * @return
	 */
	public List<HzClassCheckins> getProblemStudentId(String section, int courseid, Date date) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("courseid", courseid);
		conditions.put("section", section);
		conditions.put("currentDate", date);
		return hzClassCheckinsMapper.findByConditions(conditions);
	}

	public int autoInsert() {
		// 先找出没有当天没有点到的数据
		return hzClassCheckinsMapper.insertAutoCheckins();
	}

}
